
const webpack = require('webpack')
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin')
const path = require('path')
const env = process.env
const config = require('./src/config/env')
const createThemeColorReplacerPlugin = require('./config/plugin.config')

module.exports = {
  // 项目根路径
  publicPath: '/',
  productionSourceMap: env.NODE_ENV !== 'production',
  // 开发服务器
  devServer: {
    compress: true,
    disableHostCheck: true,
    port: 8081,
    proxy: {
      '/commonApi': {
        target: config.commonApiHost,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/commonApi': '/'
        }
      },
      '/api': {
        target: config.apiHost,
        // target: 'http://172.31.2.66:9000', // 倪周稳
        // target: 'http://172.31.2.222:9000',
        // target: 'http://172.31.1.147:9000', // 刘豪
        // target: 'http://172.31.2.114:9000',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/'
        }
      }
    }
  },
  css: {
    sourceMap: env.NODE_ENV !== 'production',
    loaderOptions: {
      less: {
        lessOptions: {
          modifyVars: {
            // less vars，customize ant design theme

            // 'primary-color': '#F5222D',
            // 'link-color': '#F5222D',
            'border-radius-base': '2px'
          },
          // DO NOT REMOVE THIS LINE
          javascriptEnabled: true
        }
      }
    }
  },
  configureWebpack: (config) => {
    if (env.NODE_ENV === 'production') {
      // dll优化
      config.plugins.push(new webpack.DllReferencePlugin({
        context: process.cwd(),
        manifest: require('./public/vendor/vendor-manifest.json')
      }))
      // 将打包出来文件动态引入index.html
      config.plugins.push(
        new AddAssetHtmlPlugin({
          // dll文件位置
          filepath: path.resolve(__dirname, './public/vendor/*.js'),
          // dll 引用路径
          publicPath: './vendor',
          // dll最终输出的目录
          outputPath: './vendor'
        })
      )
    }
    config.plugins.push(createThemeColorReplacerPlugin())
  },

  chainWebpack: config => {
    if (env.ANALYZE) {
      config
        .plugin('webpack-bundle-analyzer')
        .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
    }
  },
}
